package com.hnair.bot.controller.sys;

import com.hnair.bot.base.BaseController;
import com.hnair.bot.service.IPictureService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传公用方法
 * @author zhulei
 * @time 2017年07月23号 12:47:23
 */
@Controller
public class ImageUploadController extends BaseController {

	private final Logger logger = LoggerFactory.getLogger(ImageUploadController.class);
	
	@Autowired
	private IPictureService pictureService;
	
	/**
	 * 上传图片 到FastDFS
	 * @param file
	 * @param request
	 * @return Image
	 * 
	 */
	@RequestMapping(value = { "/rest/uploadToFastDFS" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, String>  uploadToFastDFS(HttpServletRequest request, @RequestParam("photo") MultipartFile file ) {
		Map<String, String> res = new HashMap<String, String>();
		String imgUri = null;
		try {
	    	if(file!=null && !file.isEmpty()){
				imgUri = pictureService.uploadPic(file);
				res.put("states", "0");
				res.put("imgUri", imgUri);
			}else{
				res.put("states", "1");
				res.put("msg", "请选择图片再上传");
			}
		} catch (Exception e) {
			logger.error("error:",e);
			res.put("states", "1");
			res.put("msg", "上传失败");
		}
		return res;

	}
}
